package com.softtown.modules.sys.service.impl;

import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.security.user.UserDetail;
import com.softtown.modules.sys.dao.CompSysUserEmplDao;
import com.softtown.modules.sys.dao.SysCompDao;
import com.softtown.modules.sys.dto.SysCompDTO;
import com.softtown.modules.sys.entity.*;
import com.softtown.modules.sys.enums.StatusEnum;
import com.softtown.modules.sys.service.*;
import com.softtown.modules.sys.vo.SysCompanyVO;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 公司ServiceImpl
 *
 * @author Dingyayong
 * @version v1.0
 * @create 2019-05-20 17:27
 **/
@Service
public class SysCompServiceImpl extends BaseServiceImpl<SysCompDao, SysCompEntity> implements SysCompService {

    @Resource
    private CompSysUserEmplService compSysUserEmplService;

    @Resource
    private CompSysUserEmplDao compSysUserEmplDao;

    @Resource
    private SysCompMenuService sysCompMenuService;

    @Override
    public List<SysCompDTO> getListByIds() {
        UserDetail user = SecurityUser.getUser();
        // 获取用户公司信息
        List<CompSysUserEmplEntity> compSysUserEmplEntities = compSysUserEmplDao.findCompByUserId(user.getId());
        // 获取公司ids
        // 公司所属ID
        List<Long> companyIds = new ArrayList<>();
        for (CompSysUserEmplEntity compSysUserEmplEntity : compSysUserEmplEntities) {
            companyIds.add(compSysUserEmplEntity.getCompId());
        }
        List<SysCompDTO> compDTOList = new ArrayList<>();
        // 判断是否有公司
        if (compSysUserEmplEntities.size() <= 0) {
            return compDTOList;
        }
        //获取公司信息
        compDTOList = ConvertUtils.sourceToTarget(baseDao.getSysCompEntityListByCompanyIds(companyIds), SysCompDTO.class);
        return compDTOList;
    }

    /**
     * @param @param [companyId]
     * @return @return com.softtown.modules.sys.vo.SysCompanyVO
     * @throws
     * @Title: getCompanyInfo
     * @Description: 根据公司id获取信息Service实现方法
     * @author fuming
     * @date 2019/6/28 9:26
     */
    @Override
    public SysCompanyVO getCompanyInfo(Long companyId) {
        // 获取公司信息
        return ConvertUtils.sourceToTarget(baseDao.getCompanyById(companyId), SysCompanyVO.class);
    }

    @Override
    public SysCompEntity getCompanyByCampanyName(String campanyName) {
        return baseDao.getCompanyByCampanyName(campanyName);
    }

    @Override
    public SysCompEntity getCompanyByUscc(String uscc) {
        return baseDao.getCompanyByUscc(uscc);
    }

    /**
     * @param @param [compDTO]
     * @return @return void
     * @throws
     * @Title: register
     * @Description: 注册Service实现方法
     * @author fuming
     * @date 2019/5/31 17:19
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void register(SysCompDTO compDTO) {
        Long meunId = 1088611641903702031L;
        Long meunId1 = 1088611641903702032L;
        SysCompEntity compEntity = ConvertUtils.sourceToTarget(compDTO, SysCompEntity.class);
        compEntity.setUscc(compDTO.getUscc());
        compEntity.setName(compDTO.getName());
        compEntity.setAddress(compDTO.getAddress());
        compEntity.setTel(compDTO.getTel());
        compEntity.setContact(compDTO.getContact());
        compEntity.setMobile(compDTO.getMobile());
        compEntity.setAdminUserId(compDTO.getAdminUserId());
        compEntity.setStatus(1);
        insert(compEntity);
        SysCompEntity compEntity1 = baseDao.getCompanyByUscc(compDTO.getUscc());
        CompSysUserEmplEntity compSysUserEmplEntity = new CompSysUserEmplEntity();
        compSysUserEmplEntity.setUserId(compDTO.getAdminUserId());
        compSysUserEmplEntity.setCompId(compEntity1.getId());
        compSysUserEmplEntity.setStatus(StatusEnum.ACTIVATESTATUS.value());
        compSysUserEmplService.insert(compSysUserEmplEntity);
        SysCompMenuEntity sysCompMenuEntity = new SysCompMenuEntity();
        SysCompMenuEntity sysCompMenuEntity1 = new SysCompMenuEntity();
        sysCompMenuEntity.setComId(compEntity1.getId());
        sysCompMenuEntity.setMenuId(meunId);
        sysCompMenuEntity.setCreator(SecurityUser.getUserId());
        sysCompMenuEntity.setCreateDate(new Date());
        sysCompMenuEntity.setUpdater(SecurityUser.getUserId());
        sysCompMenuEntity.setUpdateDate(new Date());
        sysCompMenuEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        sysCompMenuEntity1.setMenuId(meunId1);
        sysCompMenuEntity1.setComId(compEntity1.getId());
        sysCompMenuEntity1.setCreator(SecurityUser.getUserId());
        sysCompMenuEntity1.setCreateDate(new Date());
        sysCompMenuEntity1.setUpdater(SecurityUser.getUserId());
        sysCompMenuEntity1.setUpdateDate(new Date());
        sysCompMenuEntity1.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String date = "2099-12-31 23:59:59";
        try {
            sysCompMenuEntity1.setExprDate(dateFormat.parse(date));
            sysCompMenuEntity.setExprDate(dateFormat.parse(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        sysCompMenuService.insert(sysCompMenuEntity1);
        sysCompMenuService.insert(sysCompMenuEntity);
    }

    /**
     * @param @param [companyName]
     * @return @return com.softtown.modules.sys.dto.SysCompDTO
     * @throws
     * @Title: getCompInfo
     * @Description: 通过公司名称来获取公司信息Service实现方法
     * @author fuming
     * @date 2019/5/31 17:18
     */
    @Override
    public SysCompEntity getCompInfo(String companyName) {
        //获取公司信息
        return baseDao.getCompanyByCampanyName(companyName);
    }

    /**
     * @param @param [compDTO]
     * @return @return void
     * @throws
     * @Title: updateCompInfo
     * @Description: 公司信息修改Service实现方法
     * @author fuming
     * @date 2019/6/1 17:26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCompInfo(SysCompDTO compDTO) {
        SysCompEntity compEntity = ConvertUtils.sourceToTarget(compDTO, SysCompEntity.class);
        compEntity.setId(compDTO.getId());
        compEntity.setName(compDTO.getName());
        compEntity.setUscc(compDTO.getUscc());
        compEntity.setAddress(compDTO.getAddress());
        compEntity.setTel(compDTO.getTel());
        compEntity.setContact(compDTO.getContact());
        compEntity.setMobile(compDTO.getMobile());
        compEntity.setAdminUserId(compDTO.getAdminUserId());
        compEntity.setUpdater(compDTO.getUpdater());
        compEntity.setUpdateDate(new Date());
        compEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        baseDao.updateCompanyInfo(compEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void transfer(Long compId, Long userId) {
        SysCompEntity sysCompEntity = baseDao.getCompanyById(compId);
        sysCompEntity.setAdminUserId(userId);
        sysCompEntity.setUpdateDate(new Date());
        sysCompEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        baseDao.updateCompanyInfo(sysCompEntity);
    }

    /**
     * @param @param [adminUserId]
     * @return @return java.util.List<com.softtown.modules.sys.vo.SysCompanyVO>
     * @throws
     * @Title: getCompNameListByUserId
     * @Description: 根据用户ID查找相应的公司ID和名称Service实现方法
     * @author fuming
     * @date 2019/7/1 21:36
     */
    @Override
    public List<SysCompanyVO> getCompNameListByUserId(Long userId) {
        List<SysCompEntity> compNameListByUserId = baseDao.getCompNameListByUserId(userId);
        return ConvertUtils.sourceToTarget(compNameListByUserId, SysCompanyVO.class);
    }

    @Override
    public SysCompEntity getCompanyById(Long id) {
        return baseDao.getCompanyById(id);
    }
}

